package cn.hncu.booksManagementSystem.in.business.ebo;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import cn.hncu.booksManagementSystem.book.business.ebi.BookEbi;
import cn.hncu.booksManagementSystem.book.business.factory.BookEbiFactory;
import cn.hncu.booksManagementSystem.common.enums.XxxIdEnum;
import cn.hncu.booksManagementSystem.in.business.ebi.InEbi;
import cn.hncu.booksManagementSystem.in.dao.dao.InDetailDAO;
import cn.hncu.booksManagementSystem.in.dao.dao.InMainDAO;
import cn.hncu.booksManagementSystem.in.dao.factory.InDetailDaoFactory;
import cn.hncu.booksManagementSystem.in.dao.factory.InMainDaoFactory;
import cn.hncu.booksManagementSystem.in.vo.InDetailModel;
import cn.hncu.booksManagementSystem.in.vo.InDetailQueryModel;
import cn.hncu.booksManagementSystem.in.vo.InMainModel;
import cn.hncu.booksManagementSystem.in.vo.InMainQueryModel;
import cn.hncu.booksManagementSystem.stock.business.ebi.StockEbi;
import cn.hncu.booksManagementSystem.stock.business.factory.StockEbiFactory;
import cn.hncu.booksManagementSystem.stock.dao.dao.StockDAO;
import cn.hncu.booksManagementSystem.stock.dao.factory.StockDaoFactory;
import cn.hncu.booksManagementSystem.stock.vo.StockModel;
import cn.hncu.booksManagementSystem.utils.GeneratorXxxID_Util;

/**
 * CreateTime: 2018年4月23日 上午10:33:22	
 * @author 宋进宇  Email:447441478@qq.com
 */
public class InEbo implements InEbi {
	//注入dao
	InMainDAO inMainDao = InMainDaoFactory.getInstanceOfInMainDAO();
	InDetailDAO inDetailDao = InDetailDaoFactory.getInstanceOfInDetail();
	StockDAO stockDao = StockDaoFactory.getStockDAO();
	//注入ebi
	BookEbi bookEbi = BookEbiFactory.getInstanceOfBookEbi();
	StockEbi stockEbi = StockEbiFactory.getStockEbi();
	@Override
	public boolean add(InMainModel inMain, List<InDetailModel> details) {
		//inMain补(id,inDate)
		inMain.setId(GeneratorXxxID_Util.getXxxId(XxxIdEnum.IN_MAIN	));
		inMain.setInDate(System.currentTimeMillis());
		boolean boo = inMainDao.add(inMain);
		if (!boo) {
			return false;
		}
		//details补(in,inId,sumMoney)
		for (InDetailModel detail : details) {
			detail.setId(GeneratorXxxID_Util.getXxxId(XxxIdEnum.IN_DETAIL));
			detail.setInId(inMain.getId());
			double inPrice = bookEbi.getBookModelById(detail.getBookId()).getInPrice();
			double sumMoney = detail.getSumNum()*inPrice;
			detail.setSumMoney(sumMoney);
			boo = inDetailDao.add(detail);
			if (!boo) {
				return false;
			}
			
			//能到这里说明本次进货有效，应该更新库存
			//先查询，再判断是添加还是更新
			StockModel stock = stockEbi.getByBookId(detail.getBookId());
			if (stock!=null) {
				stock.setSumNum(stock.getSumNum()+detail.getSumNum());
				boo = stockDao.updade(stock);
				if (!boo) {
					return false;
				}
			}else {
				//能进入这里说明库存中没有该图书库存，应该添加一条
				stock = new StockModel();
				//组织参数
				stock.setId(GeneratorXxxID_Util.getXxxId(XxxIdEnum.STOCK));
				stock.setBookId(detail.getBookId());
				stock.setSumNum(detail.getSumNum());
				stock.setBookName(bookEbi.getBookModelById(detail.getBookId()).getName());
				boo = stockDao.add(stock);
				if (!boo) {
					return false;
				}
			}
		}
		return true;
	}
	@Override
	public Collection<InMainModel> getInMainsByCondition(InMainQueryModel imqm, InDetailQueryModel idqm) {
		//查询所有符合imqm的的进货单
		Set<InMainModel> set = new HashSet<InMainModel>();
		Collection<InMainModel> col = inMainDao.getInMainsByCondition(imqm);
		set.addAll(col);
		//查询所有符合idqm的进货单
		Set<InMainModel> set2 = new HashSet<InMainModel>();
		Collection<InDetailModel> col2 = inDetailDao.getInDetailsByCondition(idqm);
		for (InDetailModel detail : col2) {
			set2.add(inMainDao.getInMainInfoById(detail.getInId()));
		}
		//两个set的交集就是两个查询条件都符合的查询结果
		set.retainAll(set2);
		//下面是为了给集合末尾加个null,以便ui层实现点击JList空白部分能出现弹框
		//因为HashSet不能加null;
		col= new ArrayList<InMainModel>();
		col.addAll(set);
		col.add(null);
		
		return col;
	}
}
